package com.sx.micowell.framework.view.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.mvvm.until.LogUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.RangeMonthView;

/**
 * 范围选择月视图
 * Created by huanghaibin on 2018/9/13.
 */

public class CustomRangeMonthView extends RangeMonthView {

    private int mRadius;

    private int mPadding;

    private float mSchemeBaseLine;

    private Paint mSchemeBasicPaint = new Paint();

    private Paint mBasicSelectedPaint = new Paint();

    private Paint mTextPaint = new Paint();

    public CustomRangeMonthView(Context context) {
        super(context);
        mTextPaint.setTextSize(dipToPx(context, 9));
        mTextPaint.setColor(0xff000000);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);

    }


    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mPadding = dipToPx(getContext(), 4);
        mSchemePaint.setStyle(Paint.Style.STROKE);

        mBasicSelectedPaint.setAntiAlias(true);
        mBasicSelectedPaint.setStyle(Paint.Style.FILL);
        mBasicSelectedPaint.setTextAlign(Paint.Align.CENTER);
        mBasicSelectedPaint.setFakeBoldText(true);
        mBasicSelectedPaint.setColor(0xFFDDFFF3);

        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
        mSchemeBaseLine = mRadius - metrics.descent + (metrics.bottom - metrics.top) / 2
                + dipToPx(getContext(), 1);

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme,
                                     boolean isSelectedPre, boolean isSelectedNext) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        float baselineY = mTextBaseLine + y;

        if (isSelectedPre) {
            if (isSelectedNext) {
                canvas.drawRect(x, cy - mRadius - mPadding, x + mItemWidth + mPadding,
                        cy + mRadius, mBasicSelectedPaint);
            } else {
                //最后一个，the last
                canvas.drawRoundRect(x + mPadding, cy - mRadius - mPadding, x + mItemWidth - mPadding,
                        cy + mRadius, 12, 12, mSelectedPaint);
                canvas.drawText("结束", cx - mPadding * 2 ,
                        y + mSchemeBaseLine / 2 + mPadding / 2, mTextPaint);
            }
        } else {
            if(isSelectedNext){
                canvas.drawRect(cx,cy - mRadius - mPadding, x + mItemWidth, cy + mRadius, mBasicSelectedPaint);
            }
            canvas.drawRoundRect(x + mPadding  , cy - mRadius - mPadding, x + mItemWidth - mPadding ,
                    cy + mRadius, 12, 12, mSelectedPaint);

            canvas.drawText("开始", cx - mPadding * 2 ,
                    y + mSchemeBaseLine / 2 + mPadding / 2, mTextPaint);
        }

        return false;
    }

    private float getTextWidth(String text) {
        return mTextPaint.measureText(text);
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRadius, mSchemePaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;

        boolean isInRange = isInRange(calendar);
        boolean isEnable = !onCalendarIntercept(calendar);

        if (isSelected) {
            LogUtils.d("onDrawText: isSelected = true");
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);
        } else if (hasScheme) {
            LogUtils.d("onDrawText: hasScheme = true");
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            LogUtils.d("onDrawText: isSelected = false");
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange && isEnable? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
